<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
  <%include("/common/header.html",{title:''}){}%>
  <title>试用信息</title>
  <style>
  </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a><cite>试用信息</cite></a>
      </span>
  <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
     href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon layui-icon-refresh-3"></i>
  </a>
</div>


<div class="layui-fluid">
  <div class="layui-card">
    <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
   <!-- <script type="text/html" id="toolbarDemo">
      <button class="layui-btn layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加角色
      </button>
    </script>-->
    <script type="text/html" id="table-useradmin-webuser">

      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      <!--  <a class="layui-btn   layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-cols"></i>数据权限</a>-->
    </script>

  </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
  <form action class="layui-form" lay-filter="component-form-element" id="myform">
    <!--    <div class="layui-btn-container">
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">获取选中节点数据</button>
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="setChecked">勾选指定节点</button>
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="reload">重载实例</button>
        </div>-->
    <input type="hidden" name="id">
    <div class="layui-form-item">

      <label class="layui-form-label">选择角色：</label>
      <div class="layui-input-inline">
        <select name="sysRoleCode" id="sysRoleCode" required lay-verify="required" autocomplete="off"
                class="layui-input">

        </select>
      </div>
    </div>


    <div class="layui-form-item">
      <label class="layui-form-label">数据权限：</label>
      <div class="layui-input-inline">
        <div id="test12" class="demo-tree-more"></div>
      </div>
    </div>


    <div class="layui-form-item">
      <div class="layui-input-block" style="margin-left: 240px;">
        <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
      </div>
    </div>
  </form>
</div>



<!-- 弹出查看页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin1"
     style="padding: 15px;display: none">
  <form action class="layui-form" lay-filter="component-form-element1" id="myform1">

  </form>
</div>
<%include("/common/foot.html"){}%>
<script>

  layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon', 'tree', 'util'], function () {
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var tree = layui.tree;
    var util = layui.util;
    var laydate = layui.laydate;
    var AjaxCommon = layui.AjaxCommon;
    var AliangLayui = layui.AliangLayui;
    var AliangCommon = layui.AliangCommon;

    var title = "添加角色";
    var findAll_url = 'ontrial/list.json';
    var update_url = 'role/update.json';
    var delete_url = 'ontrial/delete.json';

    var index;//定义建立富文本编辑器全局变量
    var layedit;//定义富文本编辑器layedit全局变量
    var cols = [ //表头
      /*{checkbox: true, fixed: true},
      {
        field: 'createDate',

        templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>",
        title: '发布时间',
        fixed: 'left',
      },*/
      {templet: "<div>{{d.name}}</div>", sort: 'true', title: '企业名称',},
      {templet: "<div>{{d.lxr}}</div>", sort: 'true', title: '联系人',},
      {templet: "<div>{{d.tel}}</div>", sort: 'true', title: '联系电话',},
      {templet: "<div>{{d.youxiang}}</div>", sort: 'true', title: '邮箱',},
      {templet: "<div>{{d.enurl}}</div>", sort: 'true', title: '企业官网',},
      {templet: "<div>{{d.szcs}}</div>", sort: 'true', title: '所在城市',},
      {templet: "<div>{{d.isuse}}</div>", sort: 'true', title: '是否使用过类似软件',},
      {templet: "<div>{{d.ddmd}}</div>", sort: 'true', title: '想达到的目的是',},
      {
        field: 'add_time',
        sort: 'true',
        templet: "<div>{{layui.util.toDateString(d.add_time, 'yyyy-MM-dd ')}}</div>",
        title: '提交时间',
      },
      {title: '操作', fixed: 'right', width: 150, toolbar: '#table-useradmin-webuser',}

    ];
    AliangLayui.createTable(
      "#LAY-user-manage",
      "listReload",
      null,
      title,
      findAll_url,
      true,
      [10, 15, 20, 25],
      ['filter', 'print', 'exports'],
      cols,
      null
    );


    //监听搜索
    form.on('submit(LAY-user-front-search)', function (data) {
      var field = data.field;
      //执行重载
      table.reload('listReload', {
        where: {
          term: field.search_name
        },
        page: {
          curr: 1//重新从第 1 页开始
        }
      });
    });

    //常规用法
    laydate.render({
      elem: '#test1'
    });

    function getCheckedId(jsonObj) {
      var id = "";
      $.each(jsonObj, function (index, item) {
        if (id != "") {
          id = id + "," + item.id;
        } else {
          id = item.id;
        }
        var i = getCheckedId(item.children);
        if (i != "") {
          id = id + "," + i;
        }
      });
      return id;
    }

    //按钮事件
    util.event('lay-demo', {
      getChecked: function (othis) {
        var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
        var ids = getCheckedId(checkedData);

        layer.alert(JSON.stringify(checkedData), {shade: 0});
        console.log(checkedData);
      }
      , setChecked: function () {
        tree.setChecked('demoId1', [1, 2]); //勾选指定节点
      }
      , reload: function () {
        //重载实例
        tree.reload('demoId1', {});

      }
    });


    //初始化修改页面

    function loadData(data) {

      var ret = {
        "id": data.id,
        "sysRoleCode": data.sysRoleCode.id
      };

      form.render();
      return ret;
    }

    AliangCommon.select({
      url: "role/json_role_list.json",
      elemIds: 'sysRoleCode',
      dataValue: 'id',
      dataText: 'role_name',
      defaultValue: "role_name",//默认选项（可以是具体某个option的value或text）
      otherOption: "<option value=''>选择角色</option>",//额外选项
      isLocalRender: true//开启局部渲染
    });

    //////////////////////////////////////以上是修改每个页面修改的地方
    //弹出修改页面调用初始化数据方法
    function initUpdate(data) {
      var a = layer.open({
        type: 1,
        title: title,
        content: $('#layuiadmin-form-useradmin'),
        btn: ['关闭'], //可以无限个按钮
        cancel: function (index, layero) {//取消事件
          location.replace(location.href);　//layer.open关闭刷新
        },
        success: function (layero, index) {
          layer.iframeAuto(index);
        }
      });
      layer.full(a);





      //如果是添加则重置表单，如果是编辑则给表单赋值
      if (data == 0 || data == null) {
        $.ajax({
          url: "role/menu.json",
          type: "POST",
          data: {"eid": $("#eid").val()},
          success: function (ret) {
            console.log(ret);
            var obj = [];
            if (ret) {
              obj = JSON.parse(JSON.stringify(ret.data).replace(/authorityName/g, "title").replace(/sysMenus/g, "children").replace(/menuUrl/g, "href"));
            }
            //基本演示
            tree.render({
              elem: '#test12'
              , data: obj
              , showCheckbox: true  //是否显示复选框
              , id: 'demoId1'
              , isJump: true //是否允许点击节点时弹出新窗口跳转
              , click: function (obj) {
                var data = obj.data;  //获取当前点击的节点数据
                layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
              }
            });
          },
          error: function (jqXHR, textStatus, errorThrown) {
          }
        });
        $('#myform')[0].reset();

      } else {
        $.ajax({
          url: "role/menu.json",
          type: "POST",
          data: {"eid": $("#eid").val()},
          success: function (ret) {
            console.log(ret);
            var obj = [];
            if (ret) {
              obj = JSON.parse(JSON.stringify(ret.data).replace(/authorityName/g, "title").replace(/sysMenus/g, "children").replace(/menuUrl/g, "href"));
            }
            //基本演示
            tree.render({
              elem: '#test12'
              , data: obj
              , showCheckbox: true  //是否显示复选框
              , id: 'demoId1'
              , isJump: true //是否允许点击节点时弹出新窗口跳转
              , click: function (obj) {
                var data = obj.data;  //获取当前点击的节点数据
                layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
              }
            });
            var arr = [];
            console.log(data);
            data.sysMenuSet.forEach(function (obj) {

                arr.push(obj.id);


            });
            tree.setChecked('demoId1', [...arr]); //勾选指定节点
          },
          error: function (jqXHR, textStatus, errorThrown) {
          }
        });
        layui.form.val("component-form-element", loadData(data));
      }


    }


    //弹出查看页面调用初始化数据方法
    function initUpdate1(data) {
      var a = layer.open({
        type: 1,
        title: title,
        content: $('#layuiadmin-form-useradmin1'),
        btn: ['关闭'], //可以无限个按钮
        cancel: function (index, layero) {//取消事件
          //location.replace(location.href);　//layer.open关闭刷新
        },
        success: function (layero, index) {
          layer.iframeAuto(index);
        }
      });
      layer.full(a);

      //layui.form.val("component-form-element1", loadData(data));
      layer.photos({
        photos: '#fengmian'
        , anim: 5
      });
      layer.photos({
        photos: '#xiangxi'
        , anim: 5
      });
    }


    //修改页面的内容提交到后台处理
    form.on('submit(component-form-element)', function (data) {
      postUpdate(data);
      return false;
    });
    //头工具栏事件
    table.on('toolbar(LAY-user-manage)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'add':
          initUpdate(0);
          break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length > 0) {
            //var id = new Array(); //声明数组
            for (var i in data) {
              var a = data[i].id;//得到复选框的值
              postDelete(a);
            }
            /*layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                //alert(data.id);
                postDelete(id);
            });*/
            //alert(id)
            //layer.alert(JSON.stringify(data));
            //layer.close(index);
          } else {
            layer.msg("至少选择一个进行删除");
          }
          break;
        case 'export':
          layer.msg("导出");
          break;
      }
    });

    //点击每行右工具栏“操作”里面按钮效果
    table.on('tool(LAY-user-manage)', function (obj) {
      var data = obj.data;

      if (obj.event === 'edit') {
        initUpdate(data);
      } else if (obj.event === 'view') {
        initUpdate1(data);
      } else if (obj.event === 'del') {



        layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
          // alert(data.id);
          postDelete(data.id);
        });
      }
    });


    //ajax提交删除内容
    window.postDelete = function (id) {
      AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
        if (ret == null) {
          layer.msg("删除失败", {
            icon: 5
          });
        } else {
          layer.msg("删除成功", {
            icon: 6
          });
          layer.closeAll();
          table.reload('listReload');
        }
        return ret;
      });
    };

    //ajax提交修改内容
    window.postUpdate = function (data) {
      var formData = new FormData();
      var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
      var ids = getCheckedId(checkedData);
      formData.append("data", JSON.stringify(data.field));
      formData.append("menu", ids);
      // formData.append("file", $("input[name='file']")[0].files[0]);
      $.ajax({
        url: update_url,
        type: "POST",
        //traditional:true,
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
          if (data.code == 0) {
            layer.closeAll();
            layer.msg(data.msg, {time: 600, icon: 1}, function () {
              //table.reload('listReload');
              location.replace(location.href);
            });
          } else {
            layer.msg(data.msg, {icon: 5});
          }
        },
        error: function (jqXHR, textStatus, errorThrown) {
          var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
          layer.open({
            title: "错误信息",
            type: 2,
            content: "base/error.jhtml?info=" + estr,
            btn: ['关闭'] //可以无限个按钮
          });
        }
      });
    }
  });

</script>

</body>

</html>
